angular.module('brandnew', [])
.controller('brandnewCtrl', ['$scope', 'httpService', '$interval', 'alertmsg', 'upload', '$stateParams', function($scope, httpService, $interval, alertmsg, upload, $stateParams) {
	$scope.brandnew = {
		type: 'brand',
        old_tel: user.tel
	};

    $scope.step = 1;
    $scope.nextStep = function(tel, code) {
        httpService.query('web/valid_code', {tel: tel, code: code}, 'post').then(function(res) {
            if(res.code == 200) {
                $scope.step = 2;
                $scope.count_down_time = interval;
                $scope.isDisplayTime = false;
                $interval.cancel(timer);
            } else {
                alertmsg(res.msg);
            }
        });
    };

    $scope.id = $stateParams.id;

    function getManager(id) {
        httpService.query('web/companies/' + id + '/show_manager').then(function(res) {
            if(res.code == 200) {
                $scope.brandnew.name = res.company.name;
                $scope.brandnew.logo = res.company.logo;
                $scope.brandnew.tel = res.company.tel;
                $scope.brandnew.real_name = res.user.real_name;
            }
        });
    }
    if($scope.id) { 
        $scope.brandnew.old_tel = user.tel;
        getManager($scope.id); 
    }

	// 发送手机验证码
    var regx = /^1[0-9]{10}$/;
    $scope.count_down_time = interval;
    $scope.isDisplayTime = false;
    $scope.sendCode = function(tel) {
        if(!tel) { alertmsg('请输入手机号码！');return; }
        console.log('tel', tel)
        if (regx.test(tel)) {
            httpService.query('web/send_code', { tel: tel }, 'post').then(function(res) {
                if (res.code == 200) {
                    countDown();
                } else {
                    alertmsg(res.msg);
                }
            });
        } else {
            alertmsg('请正确输入手机号码！');
        }
    };

    // 倒计时方法
    var timer;
    function countDown() {
        // 显示倒计时时间
        $scope.isDisplayTime = true;
        timer = $interval(function() {
            $scope.count_down_time -= 1;
            // 当倒计时结束时，结束显示正常
            if ($scope.count_down_time <= 0) {
                // 结束倒计时器
                $interval.cancel(timer);
                // 将倒计时重新附值
                $scope.count_down_time = interval;
                // 设置为可提交状态
                $scope.isabled = false;
                // 显示为获取验证码
                $scope.isDisplayTime = false;
            }
        }, 1000);
    };

    $scope.newbrandfirst = false;
    $scope.brandnewimages = false;
    $scope.addBrand = function(bool, brand) {
        var file = document.querySelector('#brandnewimages').files[0];
        if (!bool) {
            if(!file && !$scope.id) { alert('请上传品牌LOGO!');return; }
            var formData = new FormData(), url;
            if($scope.id) {
                url = 'web/companies/' + $scope.id + '/edit_manager';
                brand.tel = parseInt(brand.tel)
                if(file) {
                    formData = formatParams(brand, formData, file);
                } else {
                    delete brand.logo;
                    formData = formatParams(brand, formData, file);
                }
            } else {
                url = 'web/users/add_company';
                formData = formatParams(brand, formData, file);
            }
            httpService.query(url, formData, 'post', true).then(function(res) {
                if (res.code == 200) {
                    $scope.brandnew = {};
                    $scope.newbrandfirst = false;
                    window.history.go(-1);
                    alertmsg($scope.id? '修改品牌成功！': '添加品牌成功！');
                } else {
                    alertmsg(res.msg);
                }
            }, function(error) {
                $scope.newbrandfirst = false;
            });
        } else {
            $scope.newbrandfirst = true;
        }
    };

    function formatParams(brand, formData, file) {
        brand.logo = file;
        angular.forEach(brand, function(v, k) {
            formData.append(k, v);
        });
        return formData;
    }

    $(function() {
        $('#brandnewimages').change(function(e) {
            $scope.brandnewimages = false;
            $scope.$apply();
            var file = e.target.files || e.dataTransfer.files;
            var fReader = new FileReader();
            fReader.readAsDataURL(file[0]);
            fReader.onloadend = function(event) {
                var img = document.getElementById("previewImages");
                img.src = event.target.result;
            }
        });
    });

}]);